#define _POSIX_C_SOURCE 200809L
// #include "wlroots-full.hpp"
#include "output.h"
#include "util.h"
#include <time.h>

static void output_destroy_notify(struct wl_listener *listener, void *data)
{
#if 0
    struct Output *output = wl_container_of(listener, output, destroy);
    wl_list_remove(&output->link);
    wl_list_remove(&output->frame.link);
    wl_list_remove(&output->destroy.link);
    free(output);
#endif
}

static void output_frame_notify(struct wl_listener *listener, void *data)
{
#if 0
    struct Output *output = wl_container_of(listener, output, frame);

    struct wlr_output *wlr_output = output->wlr_output;
    struct Server *server = output->server;

    struct timespec now = { 0 };
    clock_gettime(CLOCK_MONOTONIC, &now);
    wlr_scene_output_send_frame_done(output->scene_output, &now);
#endif
}

static void
add_output_to_layout(struct Server *server, struct Output *output)
{
#if 0
    struct wlr_output *wlr_output = output->wlr_output;
    struct wlr_output_layout_output *layout_output =
        wlr_output_layout_add_auto(server->output_layout, wlr_output);
    if (!layout_output) {
        wlr_log(WLR_ERROR, "unable to add output to layout");
        return;
    }

    if (!output->scene_output) {
        output->scene_output =
            wlr_scene_output_create(server->scene, wlr_output);
        if (!output->scene_output) {
            wlr_log(WLR_ERROR, "unable to create scene output");
            return;
        }
        /*
         * Note: wlr_scene_output_layout_add_output() is not
         * safe to call twice, so we call it only when initially
         * creating the scene_output.
         */
        wlr_scene_output_layout_add_output(server->scene_layout,
                                           layout_output, output->scene_output);
    }
#endif
}

static void new_output_notify(struct wl_listener *listener, void *data)
{
#if 0
    Server *server = wl_container_of(listener, server, new_output);
    struct wlr_output *wlr_output = data;

    if (!wlr_output_init_render(wlr_output, server->alloc,
                                server->renderer)) {
        wlr_log(WLR_ERROR, "unable to init output renderer");
        return;
    }

    wlr_log(WLR_DEBUG, "enable output");
    wlr_output_enable(wlr_output, true);

    wlr_output_commit(wlr_output);

    Output* output = ecalloc(1, sizeof(*output));
    output->wlr_output = wlr_output;
    wlr_output->data = output;
    output->server = server;
    wlr_output_effective_resolution(wlr_output, &output->usable_area.width, &output->usable_area.height);
    wl_list_insert(&server->outputs, &output->link);

    output->destroy.notify = output_destroy_notify;
    wl_signal_add(&wlr_output->events.destroy, &output->destroy);

    output->frame.notify = output_frame_notify;
    wl_signal_add(&wlr_output->events.frame, &output->frame);

    add_output_to_layout(server, output);
#endif
}

void output_init(Server *server)
{
#if 0
    server->new_output.notify = new_output_notify;
    wl_signal_add(&server->backend->events.new_output, &server->new_output);

    server->output_layout = wlr_output_layout_create();
    if (!server->output_layout) {
        wlr_log(WLR_ERROR, "unable to create output layout");
        exit(EXIT_FAILURE);
    }
    server->scene_layout = wlr_scene_attach_output_layout(server->scene,
                                                          server->output_layout);
    if (!server->scene_layout) {
        wlr_log(WLR_ERROR, "unable to create scene layout");
        exit(EXIT_FAILURE);
    }

    wl_list_init(&server->outputs);
#endif
}

Output::Output(Server *server)
{

}
